package com.cjjd.c.feature.subscribe.nomal_hotel;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.BaseFragment;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.bean.ListResult;
import com.cjjd.c.bean.subscribe.NomalBean;
import com.cjjd.c.feature.subscribe.info.HotelDetailActivity;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.CommonUtil;
import com.cjjd.c.utils.SharePrefUtil;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/11/22.
 * <p/>
 * 描  述：常住酒店
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class HotelNomalFragment extends BaseFragment {
    @Bind(R.id.recycleview_hotel_group)
    RecyclerView recycleviewHotelGroup;
    @Bind(R.id.listview_nomal)
    ListView listViewNomal;
    private HotelNomalListviewAdapter nomalListviewAdapter;
    private ArrayList<NomalBean> listItemData;

    @Override
    public int getContentView() {
        return R.layout.fragment_home_nomal;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }


    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            getData();
        }
    }

    private void getData() {

        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("pageNow", 1 + "");
        h1.put("pageShow", 100 + "");

        Call<HttpResult<ListResult<NomalBean>>> call = service.getUsuallyHotelList(JsonUtils.convertParam("getUsuallyHotelList", h1));
        call.enqueue(new CallbackImpl<ListResult<NomalBean>>() {
            @Override
            public void responseSuccess(Call<HttpResult<ListResult<NomalBean>>> call, Response<HttpResult<ListResult<NomalBean>>> response) {
                if (listItemData.size()>0){
                    listItemData.clear();
                }
                listItemData.addAll(response.body().getData().dataList);
                nomalListviewAdapter.notifyDataSetChanged();
                CommonUtil.setListViewHeightBasedOnChildren(listViewNomal);
            }
        });
    }

    private void initView() {

        ArrayList<String> item = new ArrayList<>();
//        item.add("item1");
//        item.add("item2");
//        item.add("item3");

        ArrayList<ArrayList<String>> group = new ArrayList<>();
//        group.add(item);
//        group.add(item);
//        group.add(item);

        recycleviewHotelGroup.setLayoutManager(new LinearLayoutManager(mContext));
        HotelNomalGroupAdapter<ArrayList<String>> groupHotelNomalGroupAdapter =
                new HotelNomalGroupAdapter<>(mContext, group, R.layout.item_hotel_group);
        recycleviewHotelGroup.setAdapter(groupHotelNomalGroupAdapter);

        //recycleviewHotelGroup.setNestedScrollingEnabled(false);


        listItemData = new ArrayList<>();
        nomalListviewAdapter = new HotelNomalListviewAdapter(mContext);
        nomalListviewAdapter.setData(listItemData);
        listViewNomal.setAdapter(nomalListviewAdapter);
        listViewNomal.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        CommonUtil.setListViewHeightBasedOnChildren(listViewNomal);

        listViewNomal.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                NomalBean nomalBean = listItemData.get(position);
                Intent intent = new Intent(mContext, HotelDetailActivity.class);
                intent.putExtra("hotelUuid", nomalBean.hotelUuid);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
